﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace 逻辑
{
    public class Game
    {
        Back back;
        QiPan qiPan;
        bool over=false;
        public Game(QiPan qiPan)
        {
            this.qiPan = qiPan;
        }
        public void Buttons_Click(object sender, EventArgs e)
        {
            if (this.over == true)
            {
                MessageBox.Show("游戏已经结束在菜单请重新开始");
                return;
            }
            ClearRemark();
            Button a = sender as Button;
            int i = 0, j = 0;
            GetXY(a, ref i, ref j);
            if (a.Tag == null || (a.Tag as QiZi).Player != qiPan.CurrentPlayer)            //以后会改为是否是在下方的棋
            {
                if (qiPan.canPut == null)
                {
                    return;
                }
                else if (qiPan.x != -1 && qiPan.y != -1)
                {
                    foreach (int s in qiPan.canPut)
                    {
                        int x;
                        x = s / 10;
                        if ((s % 10 == j) && (x == i))
                        {
                            JudgCanWin(i, j);
                            GreatBack(i, j);
                            QiZiMove(i, j);
                            PlayerChange();
                            break;
                        }
                    }
                    qiPan.canPut.Clear();
                    qiPan.x = -1;
                    qiPan.y = -1;
                }
            }
            else
            {
                (a.Tag as QiZi).CanPut();
                RemarkCanPut();
            }

        }

        private void PlayerChange()
        {
            if (qiPan.CurrentPlayer == 0)
            {
                qiPan.label.Text = "红方回合";
                qiPan.label.ForeColor = Color.FromArgb(0, 200, 0, 0);
            }
            else
            {
                qiPan.label.Text = "黑方回合";
                qiPan.label.ForeColor = Color.FromArgb(0, 0, 0, 0);
            }
        }

        private void RemarkCanPut()
        {
            foreach (int s in qiPan.canPut)
            {
                qiPan.buttons1[s / 10, s % 10].Image = qiPan.imageList2.Images[0];
            }
        }

        public void ClearRemark()
        {
            foreach (int s in qiPan.canPut)
            {
                    qiPan.buttons1[s / 10, s % 10].Image = null;
            }
        }

        private void JudgCanWin(int i, int j)
        {
            Button[,] b;
            b = qiPan.buttons1;
            if (b[i, j].Tag != null)
            {
                if ((b[i, j].Tag as QiZi).name == "帅")
                {
                    MessageBox.Show("黑方获胜");
                   this.over = true;
                   qiPan.label.Text = "黑方获胜";
                   qiPan.label.ForeColor = Color.FromArgb(0, 0, 0, 0);
                }
                else if ((b[i, j].Tag as QiZi).name == "将")
                {
                    MessageBox.Show("红方获胜");
                    this.over = true;
                    qiPan.label.Text = "红方获胜";
                    qiPan.label.ForeColor = Color.FromArgb(0, 200, 0, 0);
                }
            }
        }

        private void QiZiMove(int i, int j)
        {
            Button[,] b;
            b = qiPan.buttons1;
            b[i, j].Tag = b[qiPan.x, qiPan.y].Tag;
            b[qiPan.x, qiPan.y].Tag = null;
            b[i, j].BackgroundImage = b[qiPan.x, qiPan.y].BackgroundImage;
            b[qiPan.x, qiPan.y].BackgroundImage = null;
            (b[i, j].Tag as QiZi).x = (b[i, j].Tag as QiZi).Player==1?9-i:i;
            (b[i, j].Tag as QiZi).y = j;
            qiPan.CurrentPlayer++;
            (qiPan.CurrentPlayer) %= 2;
        }

        private void GreatBack(int i, int j)
        {
            ImageList im = new ImageList() { ColorDepth = ColorDepth.Depth32Bit, ImageSize = new Size(255,255) };
            im.Images.Add(qiPan.buttons1[qiPan.x, qiPan.y].BackgroundImage); 
            if (qiPan.buttons1[i, j].BackgroundImage != null)
            {
                im.Images.Add(qiPan.buttons1[i, j].BackgroundImage);
            }
            back = new Back(im, qiPan.x, qiPan.y, i, j, qiPan.buttons1[qiPan.x, qiPan.y].Tag as QiZi, qiPan.buttons1[i, j].Tag as QiZi);
        }
        private void GetXY(Button a, ref int i, ref int j)
        {
            for (; i < 10; i++)
            {
                j = 0;
                for (; j < 9; j++)
                {
                    if (a == qiPan.buttons1[i, j])
                    {
                        if (a.Tag != null && (a.Tag as QiZi).Player == qiPan.CurrentPlayer)
                        {
                            qiPan.x = i;
                            qiPan.y = j;
                        }
                        break;

                    }
                }
                if (j < 9 && a == qiPan.buttons1[i, j])
                    break;
            }
            if (j == 10)
            {
                MessageBox.Show("按键出错");
            }
        }
        public void ReadyQiZi()
        {
            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 9; j++)
                {
                    qiPan.buttons1[i, j].Tag = null;
                    qiPan.buttons1[i, j].BackgroundImage = null;
                    qiPan.buttons1[i, j].Image = null;
                }
            qiPan.buttons1[0, 0].BackgroundImage = qiPan.imageList1.Images[11]; qiPan.buttons1[0, 0].Tag = new 车(0, 0, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[0, 1].BackgroundImage = qiPan.imageList1.Images[10]; qiPan.buttons1[0, 1].Tag = new 马(0, 1, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[0, 2].BackgroundImage = qiPan.imageList1.Images[9]; qiPan.buttons1[0, 2].Tag = new 象(0, 2, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[0, 3].BackgroundImage = qiPan.imageList1.Images[8]; qiPan.buttons1[0, 3].Tag = new 士(0, 3, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[0, 4].BackgroundImage = qiPan.imageList1.Images[7]; qiPan.buttons1[0, 4].Tag = new 帅(qiPan, qiPan.buttons1,0);
            qiPan.buttons1[0, 5].BackgroundImage = qiPan.imageList1.Images[8]; qiPan.buttons1[0, 5].Tag = new 士(0, 5, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[0, 6].BackgroundImage = qiPan.imageList1.Images[9]; qiPan.buttons1[0, 6].Tag = new 象(0, 6, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[0, 7].BackgroundImage = qiPan.imageList1.Images[10]; qiPan.buttons1[0, 7].Tag = new 马(0, 7, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[0, 8].BackgroundImage = qiPan.imageList1.Images[11]; qiPan.buttons1[0, 8].Tag = new 车(0, 8, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[2, 1].BackgroundImage = qiPan.imageList1.Images[12]; qiPan.buttons1[2, 1].Tag = new 炮(2, 1, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[2, 7].BackgroundImage = qiPan.imageList1.Images[12]; qiPan.buttons1[2, 7].Tag = new 炮(2, 7, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[3, 0].BackgroundImage = qiPan.imageList1.Images[13]; qiPan.buttons1[3, 0].Tag = new 兵(3, 0, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[3, 2].BackgroundImage = qiPan.imageList1.Images[13]; qiPan.buttons1[3, 2].Tag = new 兵(3, 2, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[3, 4].BackgroundImage = qiPan.imageList1.Images[13]; qiPan.buttons1[3, 4].Tag = new 兵(3, 4, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[3, 6].BackgroundImage = qiPan.imageList1.Images[13]; qiPan.buttons1[3, 6].Tag = new 兵(3, 6, qiPan, qiPan.buttons1,0);
            qiPan.buttons1[3, 8].BackgroundImage = qiPan.imageList1.Images[13]; qiPan.buttons1[3, 8].Tag = new 兵(3, 8, qiPan, qiPan.buttons1,0);

            qiPan.buttons1[9, 0].BackgroundImage = qiPan.imageList1.Images[4]; qiPan.buttons1[9, 0].Tag = new 车(0, 0, qiPan, qiPan.buttons2,1);
            qiPan.buttons1[9, 1].BackgroundImage = qiPan.imageList1.Images[3]; qiPan.buttons1[9, 1].Tag = new 马(0, 1, qiPan, qiPan.buttons2,1);
            qiPan.buttons1[9, 2].BackgroundImage = qiPan.imageList1.Images[2]; qiPan.buttons1[9, 2].Tag = new 象(0, 2, qiPan, qiPan.buttons2, 1);
            qiPan.buttons1[9, 3].BackgroundImage = qiPan.imageList1.Images[1]; qiPan.buttons1[9, 3].Tag = new 士(0, 3, qiPan, qiPan.buttons2, 1);
            qiPan.buttons1[9, 4].BackgroundImage = qiPan.imageList1.Images[0]; qiPan.buttons1[9, 4].Tag = new 帅(qiPan, qiPan.buttons2,1);
            qiPan.buttons1[9, 5].BackgroundImage = qiPan.imageList1.Images[1]; qiPan.buttons1[9, 5].Tag = new 士(0, 5, qiPan, qiPan.buttons2, 1);
            qiPan.buttons1[9, 6].BackgroundImage = qiPan.imageList1.Images[2]; qiPan.buttons1[9, 6].Tag = new 象(0, 6, qiPan, qiPan.buttons2, 1);
            qiPan.buttons1[9, 7].BackgroundImage = qiPan.imageList1.Images[3]; qiPan.buttons1[9, 7].Tag = new 马(0, 7, qiPan, qiPan.buttons2,1);
            qiPan.buttons1[9, 8].BackgroundImage = qiPan.imageList1.Images[4]; qiPan.buttons1[9, 8].Tag = new 车(0, 8, qiPan, qiPan.buttons2,1);
            qiPan.buttons1[7, 1].BackgroundImage = qiPan.imageList1.Images[5]; qiPan.buttons1[7, 1].Tag = new 炮(2, 1, qiPan, qiPan.buttons2,1);
            qiPan.buttons1[7, 7].BackgroundImage = qiPan.imageList1.Images[5]; qiPan.buttons1[7, 7].Tag = new 炮(2, 7, qiPan, qiPan.buttons2,1);
            qiPan.buttons1[6, 0].BackgroundImage = qiPan.imageList1.Images[6]; qiPan.buttons1[6, 0].Tag = new 兵(3, 0, qiPan, qiPan.buttons2,1);
            qiPan.buttons1[6, 2].BackgroundImage = qiPan.imageList1.Images[6]; qiPan.buttons1[6, 2].Tag = new 兵(3, 2, qiPan, qiPan.buttons2, 1);
            qiPan.buttons1[6, 4].BackgroundImage = qiPan.imageList1.Images[6]; qiPan.buttons1[6, 4].Tag = new 兵(3, 4, qiPan, qiPan.buttons2, 1);
            qiPan.buttons1[6, 6].BackgroundImage = qiPan.imageList1.Images[6]; qiPan.buttons1[6, 6].Tag = new 兵(3, 6, qiPan, qiPan.buttons2, 1);
            qiPan.buttons1[6, 8].BackgroundImage = qiPan.imageList1.Images[6]; qiPan.buttons1[6, 8].Tag = new 兵(3, 8, qiPan, qiPan.buttons2, 1);
        }
        public void ReStar()
        {
            if (this.over == false)
            {
                DialogResult dr = MessageBox.Show("胜负未分是否真的要重新开始", "重新开始", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    DoReStar();
                    return;
                }
                else
                {
                    return;
                }
            }
            else
            {
                DoReStar();
            }
        }

        private void DoReStar()
        {
            ReadyQiZi();
            this.over = false;
            qiPan.CurrentPlayer = 1;
            PlayerChange();
            back = null;
        }
        public void 黑方投降()
        {
            if (this.over == false)
            {
                this.over = true;
                MessageBox.Show("红方获胜");
            }
            else
            {
                MessageBox.Show("游戏已经结束请从菜单重新开始");
            }
        }
        public void 红方投降()
        {
            if (this.over == false)
            {
                this.over = true;
                MessageBox.Show("黑方获胜");
            }
            else
            {
                MessageBox.Show("游戏已经结束请从菜单重新开始");
            }
        }
        public void UsingBack()
        {
            if (over == true)
            {
                MessageBox.Show("游戏已经结束在菜单请重新开始");
                return;
            }
            if (back == null)
            {
                MessageBox.Show("你还没有开始下");
                return;
            }
            if (back.i == 1)
            {
                DialogResult dr = MessageBox.Show("落子无悔真英雄，你真的要悔棋吗？", "是否悔棋", MessageBoxButtons.YesNo);
                if (dr == DialogResult.No)
                {
                    return;
                }
                qiPan.buttons1[back.x0, back.y0].BackgroundImage = back.image.Images[0];
                if (back.image.Images.Count > 1)
                {
                    qiPan.buttons1[back.x1, back.y1].BackgroundImage = back.image.Images[1];
                }
                else
                {
                    qiPan.buttons1[back.x1, back.y1].BackgroundImage = null;
                }
                if (back.q0 != null)
                {
                    if (back.q0.Player == 1)
                    {
                        back.q0.x = 9 - back.x0;
                        back.q0.y =  back.y0;
                    }
                    else
                    {
                        back.q0.x = back.x0;
                        back.q0.y = back.y0;
                    }

                }
                if (back.q1 != null)
                {
                    if (back.q1.Player == 0)
                    {
                        back.q1.x = back.x1;
                        back.q1.y = back.y1;
                    }
                    else
                    {
                        back.q1.x = 9 - back.x1;
                        back.q1.y =  back.y1;
                    }
                }
                qiPan.buttons1[back.x0, back.y0].Tag = back.q0;
                qiPan.buttons1[back.x1, back.y1].Tag = back.q1;
                qiPan.CurrentPlayer += 1;
                qiPan.CurrentPlayer %= 2;
                back.i = 0;
            }
            else
            {
                MessageBox.Show("不能连续悔棋哦");
            }
        }
        static public int ChangeDirection(int y, int v)
        {
            if (y == 1)
            {
                v = 9 - v;
            }
            return v;
        }
    }
}
